home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / tests / format.test < prev    next >
Encoding:
Text File  |  1997-08-15  |  14.7 KB  |  419 lines  |  [TEXT/ALFA]

  1. # Commands covered:  format
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1994 The Regents of the University of California.
  8. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # SCCS: @(#) format.test 1.28 97/08/11 14:45:15
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. # The following code is needed because some versions of SCO Unix have
  18. # a round-off error in sprintf which would cause some of the tests to
  19. # fail.  Someday I hope this code shouldn't be necessary (code added
  20. # 9/9/91).
  21.  
  22. set roundOffBug 0
  23. if {"[format %7.1e  68.514]" == "6.8e+01"} {
  24.     puts stdout "Note: this system has a sprintf round-off bug, some tests skipped\n"
  25.     set roundOffBug 1
  26. }
  27.  
  28. test format-1.1 {integer formatting} {
  29.     format "%*d %d %d %d" 6 34 16923 -12 -1
  30. } {    34 16923 -12 -1}
  31. test format-1.2 {integer formatting} {nonPortable} {
  32.     format "%4d %4d %4d %4d %d %#x %#X" 6 34 16923 -12 -1 14 12
  33. } {   6   34 16923  -12 -1 0xe 0XC}
  34.  
  35. # %u output depends on word length, so this test is not portable.
  36.  
  37. test format-1.3 {integer formatting} {nonPortable} {
  38.     format "%4u %4u %4u %4u %d %#o" 6 34 16923 -12 -1 0
  39. } {   6   34 16923 4294967284 -1 0}
  40. test format-1.4 {integer formatting} {
  41.     format "%-4d %-4i %-4d %-4ld" 6 34 16923 -12 -1
  42. } {6    34   16923 -12 }
  43. test format-1.5 {integer formatting} {
  44.     format "%04d %04d %04d %04i" 6 34 16923 -12 -1
  45. } {0006 0034 16923 -012}
  46. test format-1.6 {integer formatting} {
  47.     format "%00*d" 6 34
  48. } {000034}
  49.  
  50. # Printing negative numbers in hex or octal format depends on word
  51. # length, so these tests are not portable.
  52.  
  53. test format-1.7 {integer formatting} {nonPortable} {
  54.     format "%4x %4x %4x %4x" 6 34 16923 -12 -1
  55. } {   6   22 421b fffffff4}
  56. test format-1.8 {integer formatting} {nonPortable} {
  57.     format "%#x %#X %#X %#x" 6 34 16923 -12 -1
  58. } {0x6 0X22 0X421B 0xfffffff4}
  59. test format-1.9 {integer formatting} {nonPortable} {
  60.     format "%#20x %#20x %#20x %#20x" 6 34 16923 -12 -1
  61. } {                 0x6                 0x22               0x421b           0xfffffff4}
  62. test format-1.10 {integer formatting} {nonPortable} {
  63.     format "%-#20x %-#20x %-#20x %-#20x" 6 34 16923 -12 -1
  64. } {0x6                  0x22                 0x421b               0xfffffff4          }
  65. test format-1.11 {integer formatting} {nonPortable} {
  66.     format "%-#20o %#-20o %#-20o %#-20o" 6 34 16923 -12 -1
  67. } {06                   042                  041033               037777777764        }
  68.  
  69. test format-2.1 {string formatting} {
  70.     format "%s %s %c %s" abcd {This is a very long test string.} 120 x
  71. } {abcd This is a very long test string. x x}
  72. test format-2.2 {string formatting} {
  73.     format "%20s %20s %20c %20s" abcd {This is a very long test string.} 120 x
  74. } {                abcd This is a very long test string.                    x                    x}
  75. test format-2.3 {string formatting} {
  76.     format "%.10s %.10s %c %.10s" abcd {This is a very long test string.} 120 x
  77. } {abcd This is a  x x}
  78. test format-2.4 {string formatting} {
  79.     format "%s %s %% %c %s" abcd {This is a very long test string.} 120 x
  80. } {abcd This is a very long test string. % x x}
  81.  
  82. test format-3.1 {e and f formats} {
  83.     format "%e %e %e %e" 34.2e12 68.514 -.125 -16000. .000053
  84. } {3.420000e+13 6.851400e+01 -1.250000e-01 -1.600000e+04}
  85. test format-3.2 {e and f formats} {
  86.     format "%20e %20e %20e %20e" 34.2e12 68.514 -.125 -16000. .000053
  87. } {        3.420000e+13         6.851400e+01        -1.250000e-01        -1.600000e+04}
  88. if {!$roundOffBug} {
  89.     test format-3.3 {e and f formats} {
  90.     format "%.1e %.1e %.1e %.1e" 34.2e12 68.514 -.126 -16000. .000053
  91.     } {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
  92.     test format-3.4 {e and f formats} {
  93.     format "%020e %020e %020e %020e" 34.2e12 68.514 -.126 -16000. .000053
  94.     } {000000003.420000e+13 000000006.851400e+01 -00000001.260000e-01 -00000001.600000e+04}
  95.     test format-3.5 {e and f formats} {
  96.     format "%7.1e %7.1e %7.1e %7.1e" 34.2e12 68.514 -.126 -16000. .000053
  97.     } {3.4e+13 6.9e+01 -1.3e-01 -1.6e+04}
  98.     test format-3.6 {e and f formats} {
  99.     format "%f %f %f %f" 34.2e12 68.514 -.125 -16000. .000053
  100.     } {34200000000000.000000 68.514000 -0.125000 -16000.000000}
  101. }
  102. test format-3.7 {e and f formats} {nonPortable} {
  103.     format "%.4f %.4f %.4f %.4f %.4f" 34.2e12 68.514 -.125 -16000. .000053
  104. } {34200000000000.0000 68.5140 -0.1250 -16000.0000 0.0001}
  105. test format-3.8 {e and f formats} {
  106.     format "%.4e %.5e %.6e" -9.99996 -9.99996 9.99996
  107. } {-1.0000e+01 -9.99996e+00 9.999960e+00}
  108. test format-3.9 {e and f formats} {
  109.     format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
  110. } {-10.0000 -9.99996 9.999960}
  111. test format-3.10 {e and f formats} {
  112.     format "%20f %-20f %020f" -9.99996 -9.99996 9.99996
  113. } {           -9.999960 -9.999960            0000000000009.999960}
  114. test format-3.11 {e and f formats} {
  115.     format "%-020f %020f" -9.99996 -9.99996 9.99996
  116. } {-9.999960            -000000000009.999960}
  117. test format-3.12 {e and f formats} {
  118.     format "%.0e %#.0e" -9.99996 -9.99996 9.99996
  119. } {-1e+01 -1.e+01}
  120. test format-3.13 {e and f formats} {
  121.     format "%.0f %#.0f" -9.99996 -9.99996 9.99996
  122. } {-10 -10.}
  123. test format-3.14 {e and f formats} {
  124.     format "%.4f %.5f %.6f" -9.99996 -9.99996 9.99996
  125. } {-10.0000 -9.99996 9.999960}
  126. test format-3.15 {e and f formats} {
  127.     format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
  128. } {  1   1   1   1}
  129. test format-3.16 {e and f formats} {
  130.     format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
  131. } {0.0 0.1 0.0 0.0}
  132.  
  133. test format-4.1 {g-format} {
  134.     format "%.3g" 12341.0
  135. } {1.23e+04}
  136. test format-4.2 {g-format} {
  137.     format "%.3G" 1234.12345
  138. } {1.23E+03}
  139. test format-4.3 {g-format} {
  140.     format "%.3g" 123.412345
  141. } {123}
  142. test format-4.4 {g-format} {
  143.     format "%.3g" 12.3412345
  144. } {12.3}
  145. test format-4.5 {g-format} {
  146.     format "%.3g" 1.23412345
  147. } {1.23}
  148. test format-4.6 {g-format} {
  149.     format "%.3g" 1.23412345
  150. } {1.23}
  151. test format-4.7 {g-format} {
  152.     format "%.3g" .123412345
  153. } {0.123}
  154. test format-4.8 {g-format} {
  155.     format "%.3g" .012341
  156. } {0.0123}
  157. test format-4.9 {g-format} {
  158.     format "%.3g" .0012341
  159. } {0.00123}
  160. test format-4.10 {g-format} {
  161.     format "%.3g" .00012341
  162. } {0.000123}
  163. test format-4.11 {g-format} {
  164.     format "%.3g" .00001234
  165. } {1.23e-05}
  166. test format-4.12 {g-format} {
  167.     format "%.4g" 9999.6
  168. } {1e+04}
  169. test format-4.13 {g-format} {
  170.     format "%.4g" 999.96
  171. } {1000}
  172. test format-4.14 {g-format} {
  173.     format "%.3g" 1.0
  174. } {1}
  175. test format-4.15 {g-format} {
  176.     format "%.3g" .1
  177. } {0.1}
  178. test format-4.16 {g-format} {
  179.     format "%.3g" .01
  180. } {0.01}
  181. test format-4.17 {g-format} {
  182.     format "%.3g" .001
  183. } {0.001}
  184. test format-4.18 {g-format} {
  185.     format "%.3g" .00001
  186. } {1e-05}
  187. test format-4.19 {g-format} {
  188.     format "%#.3g" 1234.0
  189. } {1.23e+03}
  190. test format-4.20 {g-format} {
  191.     format "%#.3G" 9999.5
  192. } {1.00E+04}
  193.  
  194. test format-5.1 {floating-point zeroes} {
  195.     format "%e %f %g" 0.0 0.0 0.0 0.0
  196. } {0.000000e+00 0.000000 0}
  197. test format-5.2 {floating-point zeroes} {
  198.     format "%.4e %.4f %.4g" 0.0 0.0 0.0 0.0
  199. } {0.0000e+00 0.0000 0}
  200. test format-5.3 {floating-point zeroes} {
  201.     format "%#.4e %#.4f %#.4g" 0.0 0.0 0.0 0.0
  202. } {0.0000e+00 0.0000 0.000}
  203. test format-5.4 {floating-point zeroes} {
  204.     format "%.0e %.0f %.0g" 0.0 0.0 0.0 0.0
  205. } {0e+00 0 0}
  206. test format-5.5 {floating-point zeroes} {
  207.     format "%#.0e %#.0f %#.0g" 0.0 0.0 0.0 0.0
  208. } {0.e+00 0. 0.}
  209. test format-5.6 {floating-point zeroes} {
  210.     format "%3.0f %3.0f %3.0f %3.0f" 0.0 0.0 0.0 0.0
  211. } {  0   0   0   0}
  212. test format-5.7 {floating-point zeroes} {
  213.     format "%3.0f %3.0f %3.0f %3.0f" 1.0 1.1 1.01 1.001
  214. } {  1   1   1   1}
  215. test format-5.8 {floating-point zeroes} {
  216.     format "%3.1f %3.1f %3.1f %3.1f" 0.0 0.1 0.01 0.001
  217. } {0.0 0.1 0.0 0.0}
  218.  
  219. test format-6.1 {various syntax features} {
  220.     format "%*.*f" 12 3 12.345678901
  221. } {      12.346}
  222. test format-6.2 {various syntax features} {
  223.     format "%0*.*f" 12 3 12.345678901
  224. } {00000012.346}
  225. test format-6.3 {various syntax features} {
  226.     format "\*\t\\n"
  227. } {*    \n}
  228.  
  229. test format-7.1 {error conditions} {
  230.     catch format
  231. } 1
  232. test format-7.2 {error conditions} {
  233.     catch format msg
  234.     set msg
  235. } {wrong # args: should be "format formatString ?arg arg ...?"}
  236. test format-7.3 {error conditions} {
  237.     catch {format %*d}
  238. } 1
  239. test format-7.4 {error conditions} {
  240.     catch {format %*d} msg
  241.     set msg
  242. } {not enough arguments for all format specifiers}
  243. test format-7.5 {error conditions} {
  244.     catch {format %*.*f 12}
  245. } 1
  246. test format-7.6 {error conditions} {
  247.     catch {format %*.*f 12} msg
  248.     set msg
  249. } {not enough arguments for all format specifiers}
  250. test format-7.7 {error conditions} {
  251.     catch {format %*.*f 12 3}
  252. } 1
  253. test format-7.8 {error conditions} {
  254.     catch {format %*.*f 12 3} msg
  255.     set msg
  256. } {not enough arguments for all format specifiers}
  257. test format-7.9 {error conditions} {
  258.     list [catch {format %*d x 3} msg] $msg
  259. } {1 {expected integer but got "x"}}
  260. test format-7.10 {error conditions} {
  261.     list [catch {format %*.*f 2 xyz 3} msg] $msg
  262. } {1 {expected integer but got "xyz"}}
  263. test format-7.11 {error conditions} {
  264.     catch {format %d 2a}
  265. } 1
  266. test format-7.12 {error conditions} {
  267.     catch {format %d 2a} msg
  268.     set msg
  269. } {expected integer but got "2a"}
  270. test format-7.13 {error conditions} {
  271.     catch {format %c 2x}
  272. } 1
  273. test format-7.14 {error conditions} {
  274.     catch {format %c 2x} msg
  275.     set msg
  276. } {expected integer but got "2x"}
  277. test format-7.15 {error conditions} {
  278.     catch {format %f 2.1z}
  279. } 1
  280. test format-7.16 {error conditions} {
  281.     catch {format %f 2.1z} msg
  282.     set msg
  283. } {expected floating-point number but got "2.1z"}
  284. test format-7.17 {error conditions} {
  285.     catch {format ab%}
  286. } 1
  287. test format-7.18 {error conditions} {
  288.     catch {format ab% 12} msg
  289.     set msg
  290. } {format string ended in middle of field specifier}
  291. test format-7.19 {error conditions} {
  292.     catch {format %q x}
  293. } 1
  294. test format-7.20 {error conditions} {
  295.     catch {format %q x} msg
  296.     set msg
  297. } {bad field specifier "q"}
  298. test format-7.21 {error conditions} {
  299.     catch {format %d}
  300. } 1
  301. test format-7.22 {error conditions} {
  302.     catch {format %d} msg
  303.     set msg
  304. } {not enough arguments for all format specifiers}
  305. test format-7.23 {error conditions} {
  306.     catch {format "%d %d" 24 xyz} msg
  307.     set msg
  308. } {expected integer but got "xyz"}
  309.  
  310. test format-8.1 {long result} {
  311.     set a {1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
  312.     format {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG %s %s} $a $a
  313. } {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1 2 3 4 5 6 7 8 9 0 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z}
  314.  
  315. test format-9.1 {"h" format specifier} {nonPortable} {
  316.     format %hd 0xffff
  317. } -1
  318. test format-9.2 {"h" format specifier} {nonPortable} {
  319.     format %hx 0x10fff
  320. } fff
  321. test format-9.3 {"h" format specifier} {nonPortable} {
  322.     format %hd 0x10000
  323. } 0
  324.  
  325. test format-10.1 {XPG3 %$n specifiers} {
  326.     format {%2$d %1$d} 4 5
  327. } {5 4}
  328. test format-10.2 {XPG3 %$n specifiers} {
  329.     format {%2$d %1$d %1$d %3$d} 4 5 6
  330. } {5 4 4 6}
  331. test format-10.3 {XPG3 %$n specifiers} {
  332.     list [catch {format {%2$d %3$d} 4 5} msg] $msg
  333. } {1 {"%n$" argument index out of range}}
  334. test format-10.4 {XPG3 %$n specifiers} {
  335.     list [catch {format {%2$d %0$d} 4 5 6} msg] $msg
  336. } {1 {"%n$" argument index out of range}}
  337. test format-10.5 {XPG3 %$n specifiers} {
  338.     list [catch {format {%d %1$d} 4 5 6} msg] $msg
  339. } {1 {cannot mix "%" and "%n$" conversion specifiers}}
  340. test format-10.6 {XPG3 %$n specifiers} {
  341.     list [catch {format {%2$d %d} 4 5 6} msg] $msg
  342. } {1 {cannot mix "%" and "%n$" conversion specifiers}}
  343. test format-10.7 {XPG3 %$n specifiers} {
  344.     list [catch {format {%2$d %3d} 4 5 6} msg] $msg
  345. } {1 {cannot mix "%" and "%n$" conversion specifiers}}
  346. test format-10.8 {XPG3 %$n specifiers} {
  347.     format {%2$*d %3$d} 1 10 4
  348. } {         4 4}
  349. test format-10.9 {XPG3 %$n specifiers} {
  350.     format {%2$.*s %4$d} 1 5 abcdefghijklmnop 44
  351. } {abcde 44}
  352. test format-10.10 {XPG3 %$n specifiers} {
  353.     list [catch {format {%2$*d} 4} msg] $msg
  354. } {1 {"%n$" argument index out of range}}
  355. test format-10.11 {XPG3 %$n specifiers} {
  356.     list [catch {format {%2$*d} 4 5} msg] $msg
  357. } {1 {"%n$" argument index out of range}}
  358. test format-10.12 {XPG3 %$n specifiers} {
  359.     list [catch {format {%2$*d} 4 5 6} msg] $msg
  360. } {0 {    6}}
  361.  
  362. test format-11.1 {negative width specifiers} {
  363.     format "%*d" -47 25
  364. } {25}
  365. test format-12.1 {tcl_precision fuzzy comparison} {
  366.     catch {unset a}
  367.     catch {unset b}
  368.     catch {unset c}
  369.     catch {unset d}
  370.     set a 0.0000000000001
  371.     set b 0.00000000000001
  372.     set c 0.00000000000000001
  373.     set d [expr $a + $b + $c]
  374.     format {%0.10f %0.12f %0.15f %0.17f} $d $d $d $d
  375. } {0.0000000000 0.000000000000 0.000000000000110 0.00000000000011001}
  376. test format-12.2 {tcl_precision fuzzy comparison} {
  377.     catch {unset a}
  378.     catch {unset b}
  379.     catch {unset c}
  380.     catch {unset d}
  381.     set a 0.000000000001
  382.     set b 0.000000000000005
  383.     set c 0.0000000000000008
  384.     set d [expr $a + $b + $c]
  385.     format {%0.10f %0.12f %0.15f %0.17f} $d $d $d $d
  386. } {0.0000000000 0.000000000001 0.000000000001006 0.00000000000100580}
  387. test format-12.3 {tcl_precision fuzzy comparison} {
  388.     catch {unset a}
  389.     catch {unset b}
  390.     catch {unset c}
  391.     set a 0.00000000000099
  392.     set b 0.000000000000011
  393.     set c [expr $a + $b]
  394.     format {%0.10f %0.12f %0.15f %0.17f} $c $c $c $c
  395. } {0.0000000000 0.000000000001 0.000000000001001 0.00000000000100100}
  396. test format-12.4 {tcl_precision fuzzy comparison} {
  397.     catch {unset a}
  398.     catch {unset b}
  399.     catch {unset c}
  400.     set a 0.444444444444
  401.     set b 0.33333333333333
  402.     set c [expr $a + $b]
  403.     format {%0.10f %0.12f %0.15f %0.16f} $c $c $c $c
  404. } {0.7777777778 0.777777777777 0.777777777777330 0.7777777777773300}
  405. test format-12.5 {tcl_precision fuzzy comparison} {
  406.     catch {unset a}
  407.     catch {unset b}
  408.     catch {unset c}
  409.     set a 0.444444444444
  410.     set b 0.99999999999999
  411.     set c [expr $a + $b]
  412.     format {%0.10f %0.12f %0.15f} $c $c $c
  413. } {1.4444444444 1.444444444444 1.444444444443990}
  414. catch {unset a}
  415. catch {unset b}
  416. catch {unset c}
  417. catch {unset d}
  418. return
  419.